package com.andy.service.impl;

import com.andy.dao.TransferDao;
import com.andy.dao.entity.TAccount;
import com.andy.infrastructure.aop.annotation.Transaction;
import com.andy.infrastructure.ioc.annotation.Autowire;
import com.andy.infrastructure.ioc.annotation.Service;
import com.andy.service.TransferService;

@Service
public class TransferServiceImpl implements TransferService {

    //注入dao 可以改变name，让容器在初始化的时候注入其他实例bean（比如那个mockDao）
    @Autowire(name = "transferDaoImpl")
    private TransferDao transferDao;


    @Override
    @Transaction
    public void transferMoney(String fromCardNo, String toCardNo, Double amount) throws Exception {
        //先把卡查询出来
        TAccount from = transferDao.queryAccountByCardNo(fromCardNo);
        TAccount to = transferDao.queryAccountByCardNo(toCardNo);

        //赋值
        from.setBalanceAmount(from.getBalanceAmount()-amount);
        to.setBalanceAmount(to.getBalanceAmount()+amount);

        //保存
        transferDao.updateCustBalanceAmount(from);

        //故意抛错，测试是否回滚
        if(amount==100){
            throw new RuntimeException("100块钱不让转");
        }

        transferDao.updateCustBalanceAmount(to);
    }
}
